package com.scalesoft.artsync.frontend;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import com.scalesoft.artsync.plugins.BasicAccountSettings;

public class SettingsDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = 529752803699089540L;

	BasicAccountSettings settings;

	public SettingsDialog(Frame parent, String pluginName, BasicAccountSettings settings) {
		super(parent, true);
		this.settings = settings;

		// {{INIT_CONTROLS
		setTitle("Security");
		getContentPane().setLayout(new GridBagLayout());
		setSize(500, 130);
		setVisible(false);
		GridBagConstraints c = new GridBagConstraints();
		JLabel1.setText(pluginName + " username:");
		c.gridx = 0;
		c.gridy = 0;
		getContentPane().add(JLabel1, c);
		JLabel2.setText(pluginName + " password:");
		c.gridx = 0;
		c.gridy = 1;
		getContentPane().add(JLabel2, c);
		_uid.setText(settings.getUsername());
		c.gridx = 1;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		getContentPane().add(_uid, c);
		c.gridx = 1;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		getContentPane().add(_pwd, c);
		_pwd.setText(settings.getPassword());
		_ok.setText("OK");
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0;
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.WEST;
		getContentPane().add(_ok, c);
		_cancel.setText("Cancel");
		c.gridx = 1;
		c.gridy = 2;
		c.anchor = GridBagConstraints.EAST;
		getContentPane().add(_cancel, c);
		// }}

		// {{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		_ok.addActionListener(lSymAction);
		_cancel.addActionListener(lSymAction);
		// }}
	}

	public void setVisible(boolean b) {
		if (b)
			setLocation(50, 50);
		super.setVisible(b);
	}

	public void addNotify() {
		// Record the size of the window prior to calling parents addNotify.
		Dimension size = getSize();

		super.addNotify();

		if (frameSizeAdjusted)
			return;
		frameSizeAdjusted = true;

		// Adjust size of frame according to the insets
		Insets insets = getInsets();
		setSize(insets.left + insets.right + size.width, insets.top + insets.bottom + size.height);
	}

	// Used by addNotify
	boolean frameSizeAdjusted = false;

	// {{DECLARE_CONTROLS
	javax.swing.JLabel JLabel1 = new javax.swing.JLabel();

	javax.swing.JLabel JLabel2 = new javax.swing.JLabel();

	/**
	 * The user ID entered.
	 */
	javax.swing.JTextField _uid = new javax.swing.JTextField();

	/**
	   */
	javax.swing.JButton _ok = new javax.swing.JButton();

	/**
	 * The password is entered.
	 */
	javax.swing.JPasswordField _pwd = new javax.swing.JPasswordField();

	javax.swing.JButton _cancel = new javax.swing.JButton();

	// }}

	class SymAction implements java.awt.event.ActionListener {
		public void actionPerformed(java.awt.event.ActionEvent event) {
			Object object = event.getSource();
			if (object == _ok)
				Ok_actionPerformed(event);
			else if (object == _cancel)
				Cancel_actionPerformed(event);
		}
	}

	/**
	 * Called when ok is clicked.
	 * 
	 * @param event
	 */
	void Ok_actionPerformed(java.awt.event.ActionEvent event) {
		settings.saveSettings(_uid.getText(), new String(_pwd.getPassword()));
		setVisible(false);
	}

	/**
	 * Called when cancel is clicked.
	 * 
	 * @param event
	 */
	void Cancel_actionPerformed(java.awt.event.ActionEvent event) {
		_uid.setText("");
		_pwd.setText("");
		setVisible(false);
	}
}
